package;

import nme.display.MovieClip;
import nme.display.Bitmap;
import nme.text.TextFormat;
import nme.text.TextField;
import nme.text.TextFormatAlign;
import nme.text.Font;

/**
 * ...
 * @author jonathanmoriarty
 */
class CharacterPortrait extends MovieClip
{
	private var _image:Bitmap;
	private var _name:Bitmap;
	private var _direction:Int;
	
	inline private static var NAME_DISPLAY_X_LEFT:Float = 120;
	inline private static var NAME_DISPLAY_X_RIGHT:Float = -230;
	inline private static var NAME_DISPLAY_Y:Float = 297;
	
	public function new( l_x:Float, l_y:Float, l_direction:Int ) 
	{
		super();
		x = l_x;
		y = l_y;
		_direction = l_direction;
		
		if ( _image != null )
		{
			removeChild( _image );
			_image = null;
		}
		_image = Tools.loadBitmap( "portrait_null" );
		addChild( _image );
		
		_name = null;
		
		setDirection( _direction );
	}		
	
	public function setDirection( l_newDirection:Int ):Void
	{
		if ( l_newDirection == 1 || l_newDirection == -1 )
		{
			_direction = l_newDirection;
			_image.scaleX = _direction;
			if ( _name != null )
			{
				if ( _direction == 1 )
				{
					_name.x = NAME_DISPLAY_X_LEFT;
				}
				else if ( _direction == -1 )
				{
					_name.x = NAME_DISPLAY_X_RIGHT;
				}
			}
		}
	}
	
	public function setName( l_nameIndex:Int ):Void
	{
		if ( _name != null )
		{
			removeChild( _name );
			_name = null;
		}
		if ( l_nameIndex == 0 )
		{
			_name = Tools.loadBitmap( "kliibuName" );
			addChild( _name );
		}
		else if ( l_nameIndex == 1 )
		{
			_name = Tools.loadBitmap( "kwigaanName" );
			addChild( _name );
		}
		else if ( l_nameIndex == 2 )
		{
			_name = Tools.loadBitmap( "quarrenName" );
			addChild( _name );
		}
		else if ( l_nameIndex == -1 )
		{
			_name = Tools.loadBitmap( "unknownName" );
			addChild( _name );
		}
		
		if ( _name != null )
		{
			_name.y = NAME_DISPLAY_Y;
			if ( _direction == 1 )
			{
				_name.x = NAME_DISPLAY_X_LEFT;
			}
			else if ( _direction == -1 )
			{
				_name.x = NAME_DISPLAY_X_RIGHT;
			}
		}
	}
	
	public function changeImage( l_newImage:String ):Void
	{
		if ( _image != null )
		{
			removeChild( _image );
			_image = null;
		}
		_image = Tools.loadBitmap( l_newImage );
		addChild( _image );
		setDirection( _direction );
	}

}
